﻿using System;
using System.Collections.Generic;
using Rhizome.Contract;
using Rhizome.Contract.Diag;
using Rhizome.Model.Validation;

namespace Rhizome.Model
{
	public sealed class RandomCriterion : RhizomeElement, ICriterion, IValidate
    {
        /// <summary>
        /// 0 = 0%, 1 = 100%
        /// </summary>
        public double Value { get; set; }
        public static Random Rnd = new Random();

		bool ICriterion.Test()
        {
            if (Value == 0) return false;
            if (Value == 1) return true;
            
            double d;
            lock(Rnd) d = Rnd.NextDouble();
            return d <= Value;
        }

		#region Implementation of IValidate

		IEnumerable<IValidationResult> IValidate.GetValidationResults()
		{
			if (Value == 0) yield return ValidationResult.Warning("Random will never hit");
			if (Value == 1) yield return ValidationResult.Warning("Random will allways hit");
			if (Value < 0 || Value > 1) yield return ValidationResult.Error("Value is invalid");
		}

		#endregion
    }
}